# Maintainer: Alex Xu (Hello71) <alex_y_xu@yahoo.ca>
pkgname=libjxl
pkgver=0.8.2
pkgrel=0
_testdata_commit=d6168ffb9e1cc24007e64b65dd84d822ad1fc759
_skcms=6140cf9c51a5b636b25af28571e3086595ed47c8
pkgdesc="JPEG XL image format reference implementation"
url="https://github.com/libjxl/libjxl"
# s390x fails like most of the tests, implying it's broken
arch="all !s390x"
license="Apache-2.0"
makedepends="
	asciidoc
	brotli-dev
	clang
	cmake
	gdk-pixbuf-dev
	giflib-dev
	gflags-dev
	gtest-dev
	highway-dev
	lcms2-dev
	libjpeg-turbo-dev
	libpng-dev
	lld
	llvm
	openexr-dev
	samurai
	zlib-dev
	"
subpackages="$pkgname-doc $pkgname-static $pkgname-tools $pkgname-dev $pkgname-pixbuf-loader"
source="https://github.com/libjxl/libjxl/archive/v$pkgver/libjxl-v$pkgver.tar.gz
	libjxl-testdata-$_testdata_commit.tar.gz::https://github.com/libjxl/testdata/archive/$_testdata_commit.tar.gz
	https://dev.alpinelinux.org/archive/skcms/skcms-$_skcms.tar.xz
	no-werror.patch
	"

# with -fstack-clash-protection/-D_GLIBCXX_ASSERTIONS, a lot of these just segfault
case "$CARCH" in
x86) options="$options !check" ;;
esac

prepare() {
	default_prepare
	rmdir testdata
	ln -sfv "$srcdir"/testdata-$_testdata_commit testdata
	rmdir third_party/skcms
	ln -sfv "$srcdir"/skcms-$_skcms third_party/skcms
}

build() {
	case "$CARCH" in
	riscv64)
		;;
	*)
		# fails on riscv
		CFLAGS="$CFLAGS -flto=thin"
		CXXFLAGS="$CXXFLAGS -flto=thin"
		;;
	esac

	# clang gets way better performance with libjxl
	export CC=clang
	export CXX=clang++
	export LDFLAGS="$LDFLAGS -fuse-ld=lld"

	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DBUILD_SHARED_LIBS=ON \
		-DBUILD_TESTING="$(want_check && echo ON || echo OFF)" \
		-DJPEGXL_ENABLE_PLUGINS=ON \
		-DJPEGXL_ENABLE_PLUGIN_GIMP210=OFF \
		-DJPEGXL_ENABLE_BENCHMARK=OFF \
		-DJPEGXL_ENABLE_COVERAGE=OFF \
		-DJPEGXL_ENABLE_EXAMPLES=OFF \
		-DJPEGXL_ENABLE_FUZZERS=OFF \
		-DJPEGXL_ENABLE_SJPEG=OFF \
		-DJPEGXL_ENABLE_SKCMS=OFF \
		-DJPEGXL_ENABLE_VIEWERS=OFF \
		-DJPEGXL_FORCE_SYSTEM_GTEST=ON \
		-DJPEGXL_FORCE_SYSTEM_BROTLI=ON \
		-DJPEGXL_FORCE_SYSTEM_HWY=ON
	cmake --build build
}

check() {
	cd build
	export CTEST_PARALLEL_LEVEL=$JOBS
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest -E 'ColorManagementTestInstantiation/ColorManagementTest\.VerifyAllProfiles/ColorEncoding|DecodeTest\.ContinueFinalNonEssentialBoxTest|CodecTest\.TestRoundTrip|RoundtripLossless8LightningGradient|LosslessPNMRoundtrip'
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

tools() {
	pkgdesc="JPEG XL image format reference implementation tools (cjxl, djxl)"
	amove usr/bin/*
}

loader() {
	pkgdesc="$pkgdesc (pixbuf loader)"
	install_if="$pkgname=$pkgver-r$pkgrel gdk-pixbuf-loaders"

	amove \
		usr/lib/gdk-pixbuf-* \
		usr/share/mime \
		usr/share/thumbnailers
}

sha512sums="
769f102c46755d11a705ce47ae9e2ef49627ae708f416693bf1293aeeb713934baf4466e45b9c70b04ec1fc8b4a00d5ca7688c8ca03f0a91c9a15252c522a887  libjxl-v0.8.2.tar.gz
a48d74a1ccff404eb7675225b59965e056dc3fc09930572785d83bae03fa1c38e105336dbf5ecfc3f0995e8b8bfb1097b451ae661f2933590b2b152192c8aa2f  libjxl-testdata-d6168ffb9e1cc24007e64b65dd84d822ad1fc759.tar.gz
52b197b33a1acc1b48858c76932d96d921091d01f747a0e04989624d329f2e17e17d290ac923fb545a4f72bd7fb6890d1beca74f70adbeff3fc83a460c7c824d  skcms-6140cf9c51a5b636b25af28571e3086595ed47c8.tar.xz
93911a350d1515d9ea9a3c2ee1e1f4c6a6daa702ebd551c1a193bda1f8dea70cbe3ecfd1bdee2fc812cb2ea1c10195870ed0e447f557dc09e7c8a55466199646  no-werror.patch
"
